-- UnrealDungeon
-- Created by chengb Aug/4/2016
-- 夹层迷宫格子

-- 城镇列表
local cityList = { 1, 4, 6, 9, };

-- 初始化
function initUnrealDungeon(self)
    self.item:removeAllChildren();

    local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
    if type(dbase) == "table" then
        -- 如果配置了需要道具才出现，当包裹中没有需求的道具时隐藏格子
        if type(dbase["need_property"]) == "number" then
            if ItemM.getAmount(ME.user, dbase["need_property"]) <= 0 then
                cclog("包裹中没有道具(%d)，夹层不出现。", dbase["need_property"]);
                return;
            end
        elseif type(dbase["need_property"]) == "table" then
            for _, id in pairs(dbase["need_property"]) do
                if ItemM.getAmount(ME.user, id) <=0 then
                    cclog("包裹中没有道具(%d)，夹层不出现。", id);
                    return;
                end
            end
        end
    end

    -- 获取夹层迷宫id
    local unrealId = self.gridData.unrealId;
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 获取格子位置
            local pos = self.gridData:getPos();

            local dbase = UnrealDungeonM.query(unrealId, "dbase");

            -- 如果是勇者斗恶龙的夹层
            if dbase["brave_vs_dragon"] == 1 then
                local elemDbase = DungeonM.queryElement(self.gridData.element, "dbase");
                local lockId = elemDbase["lock_id"];
                if not BraveVSDragonM.isBuidingUnlock(lockId) then
                    -- 建筑未解锁
                    local lockDbase = BraveVSDragonM.queryLock(lockId, "dbase");
                    if type(lockDbase["lock_tip"]) == 'string' then
                        local tip = getLocStr(lockDbase["lock_tip"]);
                        local msgList = splitString(tip, "\n");
                        splitAlert(msgList);
                    else
                        alert(getLocStr("cannot_arrive"));
                    end
                    return;
                end

                local alias = BraveVSDragonM.queryLock(lockId, "alias");

                -- 如果是沼泽洞窟，并且已经解救过公主，则不允许进入
                if alias == "swamp_cave" and
                   BraveVSDragonM.hasRescuedPrincess() then
                    alert(getLocStr("cave_entrance_collapse"));
                    return;

                -- 如果是龙王城，并且还没保存过进度
                elseif alias == "dragon_city" and
                    not BraveVSDragonM.hasSavedProgress() then
                    local function okFunc()
                        -- 保存进度成功后再进入夹层
                        showCommunicatingForm(getLocStr("saving"));
                        if not BraveVSDragonM.saveProgress(pos) then
                            closeCommunicatingForm();
                        end
                    end

                    local function cancelFunc()
                        -- 直接进入夹层
                        DungeonActionM.go("start_unreal", pos, unrealId);
                    end

                    confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("slime_note"), getLocStr("save_progress_confirm"),
                        okFunc, cancelFunc);
                    return;

                elseif table.indexOf(cityList, lockId) ~= -1 then
                    -- 如果已经加入龙王，提示城镇被毁灭
                    local choose = BraveVSDragonM.getDragonKingJoinChoose();
                    if choose == BraveVSDragonM.CHOOSE_TYPE_ACCEPT then
                        alert(getLocStr("dragon_king_destory"));
                        return;
                    end
                end
            end

            local desc = UnrealDungeonM.query(unrealId, "desc");
            if desc == nil or desc == "" then
                -- 没有配置描述，则直接进入夹层
                DungeonActionM.go("start_unreal", pos, unrealId);
                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开夹层进入界面
            UIMgr.getCurrentScene():removeFormByName("UIUnrealEnter");
            local uiUnrealEnter = UIUnrealEnter.create(pos, unrealId);
            UIMgr.getCurrentScene():addForm(uiUnrealEnter);
        end
    end

    local iconIndex = UnrealDungeonM.query(unrealId, "icon");
    local iconPath = getLevelItemIconPath(iconIndex);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    isVisible = true;

    -- 如果是龙王夹层，拒绝龙王邀请后才出现
    local dbase = UnrealDungeonM.query(unrealId, "dbase");
    if dbase["dragon_king"] == 1 then
        local choose = BraveVSDragonM.getDragonKingJoinChoose();
        if choose ~= BraveVSDragonM.CHOOSE_TYPE_REFUSE_AGAIN then
            isVisible = false;
        end
    end

    self.icon:setVisible(isVisible);

    -- 如果是boss夹层，需要呼吸效果
    if dbase["is_boss"] == 1 and dbase["no_breath"] ~= 1 then
        self.needAnimation = true;
    end
end


